﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using up6.db.database;
using up6.db.database.down2.mongo;
using up6.db.model;
using up6.db.sql;
using up6.db.utils;
using up6.utils;

namespace up6.db.database.down2.sql
{
    public class SqlFile
    {
        public static SqlFile build()
        {
            var tp = ConfigReader.dbType();
            if (tp == DataBaseType.MongoDB) return new MongoFile();
            return new SqlFile();
        }

        public virtual void Add(ref DnFileInf inf)
        {
            SqlTable.build("down_files").insert(inf);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fid"></param>
        public virtual void Delete(string fid, int uid)
        {
            SqlTable.build("down_files").delete(SqlWhere.build()
                .eq("f_id", fid)
                .eq("f_uid", uid));
        }

        public virtual void process(string fid, int uid, string lenLoc, string perLoc)
        {
            SqlTable.build("down_files").update(SqlSeter.build()
                .set("f_lenLoc", lenLoc)
                .set("f_perLoc", perLoc)
                , SqlWhere.build()
                .eq("f_id", fid)
                .eq("f_uid", uid));
        }

        public virtual void Clear()
        {
            SqlTable.build("down_files").clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public virtual string all_uncmp(int uid)
        {
            var files = SqlTable.build("down_files").reads<DnFileInf>(SqlWhere.build()
                .eq("f_uid", uid)
                .eq("f_complete",false));
            return JsonConvert.SerializeObject(files);
        }

        /// <summary>
        /// 从up6_files表中加载所有已经上传完毕的文件
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public virtual string all_complete(int uid)
        {
            var fs = SqlTable.build("up6_files").reads<FileInf>(SqlWhere.build()
                .eq("f_uid", uid)
                .eq("f_deleted", false)
                .eq("f_complete", true)
                .eq("f_fdChild", false)
                .eq("f_scan", true));

            List<DnFileInf> files = new List<DnFileInf>();
            foreach(var f in fs){
                DnFileInf d = new DnFileInf();
                d.id = Guid.NewGuid().ToString("N");
                d.f_id = f.id;
                d.fdTask = f.fdTask;
                d.nameLoc = f.nameLoc;
                d.sizeLoc = f.sizeLoc;
                d.sizeSvr = f.sizeLoc;
                d.lenSvr = f.lenSvr;
                d.pathSvr = f.pathSvr;
                d.lenLocSec = f.lenLocSec;
                d.encrypt = f.encrypt;
                d.blockSize = f.blockSize;
                d.object_key = f.object_key;
                files.Add(d);
            }
            return JsonConvert.SerializeObject(files);
        }
        /// <summary>
        /// 取根目录下所有子文件
        /// 注意：传进来的必须是根目录ID
        /// </summary>
        /// <param name="pidRoot"></param>
        /// <returns></returns>
        public virtual string childs(string pidRoot)
        {
            var files = SqlTable.build("up6_files").reads<FileInf>(SqlWhere.build()
                .eq("f_pidRoot", pidRoot));

            var childs = new JArray();
            foreach (var f in files)
            {
                var o = JObject.FromObject(f);
                o.Add("f_id", f.id);
                o.Add("fields", new JObject {
                    {"object_key",PathTool.url_safe_encode(f.object_key) }
                });
                childs.Add(o);
            }
            return JsonConvert.SerializeObject(childs);
        }
    }
}